<?php
//学生个人中心操作类

class Stucenter{
        
    //显示个人中心
    public function index(){

        //显示发布者表中的头像和名称
        $stu=D('student');
        $stulist=$stu->field("id,username,head")->select();  //学生信息数组
        $this->assign('prolist',$stulist);


        //显示消息提醒条数信息
        $mess=D('messages');
        //定义查询条件数组
        $need=array("state"=>0,"torole"=>$_SESSION[loginuser][role],"fromrole"=>array(1,2,3),"too"=>$_SESSION[loginuser][id]);
        $messtotal=$mess->where($need)->total();   //与发布者相关的消息数组
        $this->assign('messtotal',$messtotal);
		//显示站内信提醒条数信息
        $leneed=array("state"=>0,"torole"=>$_SESSION[loginuser][role],"fromrole"=>0,"too"=>$_SESSION[loginuser][id]);
        $letotal=$mess->where($leneed)->total();   //与发布者相关的站内信数组
        $this->assign('letotal',$letotal);
       
	    //显示投标项目个数
        $bidor=D('bidorders');
        $where=array("role"=>$_SESSION[loginuser][role],"pidname"=>$_SESSION[loginuser][id]);
        $bidortotal=$bidor->where($where)->total();   
        $this->assign('bidortotal',$bidortotal);
		
		//显示承接项目个数
        $objor=D('objectorders');
        $where=array("role"=>$_SESSION[loginuser][role],"nameid"=>$_SESSION[loginuser][id]);
        $objortotal=$objor->where($where)->total();   
        $this->assign('objortotal',$objortotal);
		
		//显示承接项目个数
        $appli=D('application');
        $where=array("sid"=>$_SESSION[loginuser][id]);
        $applitotal=$appli->where($where)->total();   
        $this->assign('applitotal',$applitotal);
		
        //显示最新发布的前四个项目
        $obj=D("project");
        $objlist=$obj->order("addtime desc")->limit(4)->select();  //与发布者相关的项目数组
        $this->assign('objlist',$objlist);
		$this->display();
      //此方法向模板中输出当前的姓名，头像，相关的消息条数，最新发布的四个项目。 
    }

    //获取学生基本信息页
    public function edit(){
        $this->assign('list',D('student')->find($_SESSION[loginuser][id]));
        $this->display("edit");
    }
	
	//获取学生联系方式页
    public function editcontact(){
        $this->assign('list',D('student')->find($_SESSION[loginuser][id]));
        $this->display("editcontact");
    }
	
    //学生基本信息
    public function update(){
        $pro=D("student");
        if($pro->update($_POST,1,1)>0){
             $this->success("修改成功",1,"index");
        }else{
            $this->error($pro->getMsg(),2,"procenter/edit/id/".$_POST['id']);
        }
    }
	
    //获取密码设置页
    public function getsetpass(){
		//P($_SESSION);
       // P($_SESSION[loginuser][role]);
		$this->display("getsetpass");
    }
	
	//验证原密码是否正确
    public function checkoldpass(){
        $oldpass=$_POST['oldpass'];		
		$stu=D("student");
		$stuoldpass=$pro->field("pass")->find($_POST['id']); 
		//P($prooldpass);
		if($oldpass==$stuoldpass['pass']){  //密码现在数据库还未加密，暂时先这样写
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}
	
	//验证新密码是否正确
    public function checkpass(){
        $pass=$_POST['npass'];		
		if(preg_match("/^[A-Za-z0-9]{6,}$/i",$pass)==1){  
			echo "yes";
		}else{
			echo "no";
		}	
		exit();
	}
    //执行密码修改
    public function setpass(){
         /*$arr['id']=$_POST['id'];     //设置一个数组只接受新密码字段
         $arr['pass']=$_POST['pass'];*/
		$_POST['pass']=$_POST['npass'];
        if(D('student')->update()>0){
             $this->success("修改成功",1,"index");
        }else{
            $this->error("修改失败",2,"procenter/getsetpass/id/".$_POST['id']);
        }                
    }

    //验证名字是否正确
    public function checkname(){
        $name=$_POST['name'];
        if(preg_match("/^[^0-9a-zA-Z_]{4,12}$/i",$name)==1){
            echo "yes";
        }else{
            echo "no";
        }
        exit();
    }
	
	//验证手机号码是否正确和是否唯一
    public function checkphone(){
        $phone=$_POST['phone'];
		//导出数据库的手机号码进行对比
		$pro=D("student");
		$prophone=$pro->field("phone")->where(array("id !="=>$_POST['id']))->select(); //查询出以存在的邮箱
		//P($prophone);
		foreach($prophone as $v){
			if(in_array($phone,$v)){
				echo "手机号码已存在";
				exit();
			}
		}
		if(preg_match("/^1[3-8]\d{9}$/i",$phone)==1){
				echo "yes";
		}else{
				echo "no";
		}
		exit();
	}
    //获取头像设置页
    public function getsethead(){
        $this->assign('list',D('student')->find($_SESSION[loginuser][id]));
        $this->display("getsethead");
    }

    //修改头像
    public function sethead(){
		$upfile = new FileUpload();  //执行brophp中的文件上传类
		if($upfile->upload("pic")){
		   //上传成功
			$filename = $upfile->getFileName();  //获得上传文件名字
			$head['head']=$filename;
			$head['id']=$_POST['id'];
			D('student')->update($head);  //上传头像名字
		}else{
		   //上传失败
		   $filename="no";   //定义一个上传失败的值，随便定义
		}
		echo "<script>window.parent.dogetpic('{$filename}');</script>";  //不管上传成功还是失败都要调用模板中的dogetpic函数，此处为调用Iframe对应的页面的函数，需使用window.parent
		exit();      
    }

    public function docut(){
		$img = new Image();
		$img->cut($_POST['name'],$_POST['x'],$_POST['y'],$_POST['w'],$_POST['h']);
		echo "ok";
	}

	//获取工作室的列表
	public function atelierlist(){
		//封装搜索条件
		$where = array();
		$urlparam = "";
		if(!empty($_GET['name'])){
			$where['name']="%".$_GET['name']."%";
			$urlparam.="/name/".$_GET['name'];
		}
		if(!empty($_GET['direction'])){
			$where['direction']="%".$_GET['direction']."%";
			$urlparam.="/direction/".$_GET['direction'];
		}
		//创建一个工作室操作类
		$ate = D("atelier");
		//创建分页对象
		$page = new Page($ate->where($where)->total(),5,$urlparam);
		//获取每页数据
		$date = $ate->where($where)->limit($page->limit)->select();
		//将数据分配给模板
		$this->assign("atelist",$date);
		//分配分页内容给模板
		$this->assign("fpage",$page->fpage());
		//加载模板
		$this->display();
	}
	
	
	//申请加入工作室
	public function applyatelier(){
		//创建一个工作是申请操作类
		$apl = D("application");
		
		//获取自己所有申请
		$_SESSION[loginuser][id]=1;
		$apllistwait = $apl->field("ateid")->where(array("sid"=>$_SESSION[loginuser][id],"status"=>0))->select();
		$apllistrej = $apl->field("ateid")->where(array("sid"=>$_SESSION[loginuser][id],"status"=>2))->select();
		$ateid = $_GET['ateid'];
		//查询所有申请若已申请过，并还未处理的就不能再次申请，提示等待处理
		foreach($apllistwait as $v){
			if(in_array($_GET['ateid'],$v)){
				echo "error";
				exit();
			}
		}
		//查询所有申请若已申请过，且被拒绝三次，提示已被拒绝三次，不能再申请该工作室
		P($apllistrej);
		$i=0;
		foreach($apllistrej as $v){
		P($v);
			if($v['ateid'] == $ateid){
				$i++;
			}
		}
		if($i>=3){
				echo "error2";
				exit();
			}
		//拼装添加条件
		$_POST = array("sid"=>$_SESSION[loginuser][id],"ateid"=>$_GET['ateid'],"addtime"=>time());
		
		if($apl->insert()){
			$this->redirect("atelierlist");
		} 	
	}
	//获取申请加入工作室列表
	public function getapplication(){
		//封装搜索条件
		$where = array("sid"=>$_SESSION[loginuser][id]);
		$urlparam = "";
		if(!empty($_GET['name'])){
			$where['name']="%".$_GET['name']."%";
			$urlparam.="/name/".$_GET['name'];
		}
		//实例化申请列表操作类
		$app = D('application');
		//创建分页对象
		$page = new Page($app->where($where)->total(),5,$urlparam);
		//获取每页数据
		//$date = $app->where($where)->limit($page->limit)->field('id,ateid aid,addtime atime,rejectnum renum,status')->r_select(array('atelier','name aname','id'));
		$date = $app->query("select app.id,app.ateid aid,app.addtime atime,app.rejectnum renum,app.status,ate.name from plat_application app,plat_atelier ate where app.sid=".$_SESSION[loginuser][id]." and ate.id=app.ateid","select");
		//P($date);
		//将数据分配给模板
		$this->assign("application",$date);
		//分配分页内容给模板
		$this->assign("fpage",$page->fpage());
		//加载模板
		$this->display();
			
	}
	
	
}